package com.firefightingiot.firefightingiot.config.mybatisplus;

import com.firefightingiot.firefightingiot.web.service.Test;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import javax.annotation.Resource;

//@Component
//@Order(value = 1)
public class MyCoapServer implements ApplicationRunner {

    @Resource
    Test test;

//    public CoapServer getCoapServer(){
//
//    }

//    public static void main(String[] args) {
//        CoapServer server = new CoapServer();//主机为localhost 端口为默认端口5683
//        server.add(new CoapResource("data"){//创建一个资源为hello 请求格式为 主机：端口\hello
//
//            @Override
//            public void handleGET(CoapExchange exchange) { //重写处理GET请求的方法
//                exchange.respond(CoAP.ResponseCode.CONTENT, "Hello CoAP22111!");
//            }
//
//        });
//        server.add(new CoapResource("time"){ //创建一个资源为time 请求格式为 主机：端口\time
//
//            @Override
//            public void handleGET(CoapExchange exchange) {
//                Date date = new Date();
//                exchange.respond(CoAP.ResponseCode.CONTENT,
//                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
//            }
//
//        });
//        server.start();
//    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        getCoapServer();
    }
    public void getCoapServer(){
        CoapServer server = new CoapServer();//主机为localhost 端口为默认端口5683
        server.add(new CoapResource("data"){//创建一个资源为hello 请求格式为 主机：端口\hello

            @Override
            public void handleGET(CoapExchange exchange) { //重写处理GET请求的方法
                exchange.respond(CoAP.ResponseCode.CONTENT, exchange.getRequestText());
            }

            @Override
            public void handlePOST(CoapExchange exchange) {
                System.out.println("post start");
                String result = exchange.getRequestText();
                System.out.println("the received text:"+result);
                //exchange.respond(ResponseCode.CHANGED);
                exchange.respond(CoAP.ResponseCode.CONTENT, exchange.getRequestText());
            }

        });
//        server.add(new CoapResource("time"){ //创建一个资源为time 请求格式为 主机：端口\time
//
//            @Override
//            public void handleGET(CoapExchange exchange) {
//                Date date = new Date();
//                exchange.respond(CoAP.ResponseCode.CONTENT,
//                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
//            }
//
//        });
        server.start();
    }
}
